package com.dyc.service.impl;


import com.dyc.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.dyc.mapper.EmpExprMapper;
import com.dyc.mapper.EmpMapper;
import com.dyc.pojo.*;
import com.dyc.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    /**
     * 分页查询
     *
     * @param empQueryParam
     * @return
     */
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //设置分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //执行查询
        List<Emp> emplist = empMapper.list(empQueryParam);
        //解析结果,并封装
        Page<Emp> p = (Page<Emp>) emplist;
        long total = p.getTotal();
        List<Emp> result = p.getResult();

        return new PageResult<Emp>(total, result);
    }

    /**
     * 新增员工
     *
     * @param emp
     */
    @Transactional(rollbackFor = {Exception.class})//事务管理,默认运行时异常回滚,rollbackFor可以指定
    @Override
    public void add(Emp emp) {
        //1.保存员工基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        //2.保存员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!exprList.isEmpty()) {
            //遍历集合为empId赋值
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(emp.getId());
            }
            empExprMapper.insertBatch(exprList);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        //1.删除员工的基本信息
        empMapper.deleteById(ids);
        //2.删除工作经历
        empExprMapper.deleteByEmpIds(ids);
    }

    //根据id查询员工信息
    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getInfoById(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        //1.根据id修改信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //2.根据id修改工作经历(先删除)
        empExprMapper.deleteByEmpIds(Collections.singletonList(emp.getId()));
        //(后添加)
        List<EmpExpr> exprList = emp.getExprList();
        if (!exprList.isEmpty()) {
            for (EmpExpr empExpr : exprList) {
                //为每个经历赋值id
                empExpr.setEmpId(emp.getId());
            }
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public LoginInfo login(Emp emp) {
        //调用mapper查询员工信息
        log.info("根据用户名查询员工信息{}", emp);
        Emp e = empMapper.selectByUsernameAndPassword(emp);
        //判断是否存在这个员工
        if (e != null) {
            log.info("登录成功,员工信息:{}", e);
            //生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            log.info("jwt令牌为:{}", jwt);
            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), jwt);
        }
        return null;
    }


    /**
     * 修改密码
     * @param id
     * @param password
     */
    @Override
    public void changePassword(Integer id, String password) {
        empMapper.changePassword(id,password);
    }



    /*@Override
    public PageResult<Emp> page(Integer page, Integer pageSize) {
        //调用mapper接口,查询总记录数
        long count = empMapper.count();
        //调用mapper接口,查询结果列表
        Integer start = (page - 1) * pageSize;
        List<Emp> list = empMapper.list(start, pageSize);
        return new PageResult<Emp>(count,list) ;
    }*/

    /**
     * pagehelper
     *
     * @param page
     * @param pageSize
     * @return
     */
   /* @Override
    public PageResult<Emp> page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行查询
        List<Emp> emplist = empMapper.list(name, gender, begin, end);
        //解析结果,并封装
        Page<Emp> p = (Page<Emp>) emplist;
        long total = p.getTotal();
        List<Emp> result = p.getResult();

        return new PageResult<Emp>(total, result);
    }*/
}
